package com.bnz.service.impl;

import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.Student1;
import com.bnz.service.StudentService;
import com.bnz.util.DbUtils;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

public class StudentServiceImpl implements StudentService {
    private StudentMapper studentMapper;
    // 1. 查询所有学生
    @Override
    public List<Student1> findAll() throws IOException {
        try(SqlSession session = DbUtils.getSqlSession()){
            studentMapper = session.getMapper(StudentMapper.class);
            List<Student1> students = studentMapper.findAll();
            return students;
        }
    }
    // 添加学生
    @Override
    public void add(Student1 student) throws IOException {
        try(SqlSession session = DbUtils.getSqlSession()) {
            studentMapper = session.getMapper(StudentMapper.class);
            studentMapper.insert(student);
        }
    }

    // 3. 根据id查询学生
    @Override
    public Student1 findById(int sid) throws IOException {
        try(SqlSession session = DbUtils.getSqlSession()) {
            studentMapper = session.getMapper(StudentMapper.class);
            Student1 student = studentMapper.findById(sid);
            return student;
        }
    }

    @Override
    public void updateById(Student1 student) throws IOException {
        try(SqlSession session = DbUtils.getSqlSession()) {
            studentMapper = session.getMapper(StudentMapper.class);
            studentMapper.update(student);
        }
    }

    @Override
    public void deleteById(int sid) throws IOException {
        try(SqlSession session = DbUtils.getSqlSession()) {
            studentMapper = session.getMapper(StudentMapper.class);
            studentMapper.delete(sid);
            System.out.println(sid + " 删除成功");
        }
    }
}
